package com.usian.yygh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.yygh.common.result.Result;
import com.usian.yygh.common.util.MD5;
import com.usian.yygh.mapper.HospitalSetMapper;
import com.usian.yygh.model.hosp.HospitalSet;
import com.usian.yygh.service.HospitalSetService;
import com.usian.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * ClassName: HospitalSetServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/27-16:07
 * Version: 1.0
 * Description:
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    /**
     * 分页查询医院设置
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @Override
    public Result findPageHospSet(Long current, Long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> pageInfo = new Page<>(current, limit);
        //设置查询条件
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        //医院名称
        wrapper.like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()),HospitalSet::getHosname,hospitalSetQueryVo.getHosname());
        //医院编号
        wrapper.eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()),HospitalSet::getHoscode,hospitalSetQueryVo.getHoscode());
        //执行分页查询
        Page<HospitalSet> page = page(pageInfo, wrapper);
        return Result.ok(page);
    }

    /**
     * 添加医院设置
     * @param hospitalSet
     * @return
     */
    @Override
    public Result saveHospSet(HospitalSet hospitalSet) {

        //设置状态 1 使用  0 不能使用
        hospitalSet.setStatus(1);
        //设置签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //添加
        boolean save = save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 获取医院签名信息
     * @param hoscode
     * @return
     */
    @Override
    public String getKeySign(String hoscode) {
        HospitalSet hospitalSet = this.getOne(Wrappers.<HospitalSet>lambdaQuery().eq(HospitalSet::getHoscode, hoscode));
        String signKey = hospitalSet.getSignKey();
        return signKey;
    }
}
